import UserModel, {Gender, IUser, SPORT_LEVEL_ENUM} from "../models/UserModel";
import jwt from "jsonwebtoken";
import config from "@/config/config.default";

class UserService {
    /** 用户注册 */
    public async createUser(params: IUser) {
        try {
            const user = await UserModel.create(params);
            return user;
        } catch (error) {
            console.error(error);
            throw error;
        }
    }

    /** 查询用户列表 */
    public async getUserList(params: {
        pageSize: number;
        pageIndex: number;
        userName: string;
        email: string;
    }) {
        try {
            const {pageSize, pageIndex, userName, email} = params;
            const filterRules: any = {};
            if (userName) {
                filterRules.userName = {$regex: new RegExp(userName)};
            }
            if (email) {
                filterRules.email = {$regex: new RegExp(email)};
            }
            const total = await UserModel.find().countDocuments();
            const list = await UserModel.find()
                .where(filterRules)
                .skip((pageIndex - 1) * pageSize)
                .limit(pageSize)
                .select([
                    'userId',
                    'userName',
                    'password',
                    'tel',
                    'email',
                    'avatar',
                    'gender',
                    'age',
                    'activityType',
                    'sportsCategory',
                    'sportsLevel',
                    'nickname',
                    'wechatId',
                ])
                .exec();
            return {
                total,
                pageIndex,
                pageSize,
                list,
            };
        } catch (error) {
            console.error(error);
            throw error;
        }
    }

    /** 用户更新 */
    public async updateUser(params: IUser) {
        try {
            await UserModel.findOneAndUpdate({userId: params.userId}, params, {new: true});
            return true;
        } catch (error) {
            console.error(error);
            throw error;
        }
    }

    /** 用户删除 */
    public async deleteUser(userId: string) {
        try {
            await UserModel.findOneAndDelete({userId});
            return true;
        } catch (error) {
            console.error(error);
            throw error;
        }
    }

    /** TODO 用户登录 */
    // public async userLogin(params: { username: string; password: string }) {
    //   try {
    //     const user = await UserModel.findOne({ username: params.username });
    //     if (!user) {
    //       return {
    //         code: 401,
    //         message: "账号错误",
    //       };
    //     } else if (user?.password && user.password !== params.password) {
    //       return {
    //         code: 401,
    //         message: "密码错误",
    //       };
    //     } else {
    //       const token = jwt.sign(
    //         { username: params.username },
    //         config.tokenSecrets,
    //         { expiresIn: "60s" } // expiresIn 单位s m/min/minutes h/hr/hours d/days
    //       );
    //       return {
    //         code: 200,
    //         token,
    //       };
    //     }
    //   } catch (error) {
    //     console.error(error);
    //     throw error;
    //   }
    // }

    /** TODO 注销 */

    /** TODO 用户信息管理 */

    /**  获取用户信息 */
    public async getUserById(id: string) {
        try {
            const user = await UserModel.findOne({userId: id}).select([
                'userId',
                'userName',
                'password',
                'tel',
                'email',
                'avatar',
                'gender',
                'age',
                'activityType',
                'sportsCategory',
                'sportsLevel',
                'nickname',
                'wechatId',
            ]);
            return user;
        } catch (error) {
            console.error(error);
            throw error;
        }
    }
}

export default new UserService();
